<?php

/**
 * This is the model class for table "{{company_card}}".
 *
 * The followings are the available columns in table '{{company_card}}':
 * @property integer $zm_id
 * @property integer $zoa_type
 * @property integer $zca_id
 * @property string $zoa_id_card
 * @property string $zoa_id_pic
 * @property string $zoa_body_pic
 * @property string $zoa_license_pic
 * @property integer $zoa_check
 * @property integer $zoa_check_time
 * @property integer $zoa_accept
 * @property integer $zoa_accept_time
 * @property integer $zoa_deposit_status
 * @property double $zoa_deposti
 * @property integer $zoa_deposit_time
 * @property integer $zpy_id
 * @property string $zoa_orderid
 * @property integer $inputtime
 */
class CompanyCard extends CActiveRecord
{
	//顶级分类，用于筛选
	 public $category_top;
	 
	 //会员名称，用于筛选
	 public $zm_name;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CompanyCard the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{company_card}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zoa_type, zca_id, zoa_check, zoa_check_time, zoa_accept, zoa_accept_time, zoa_deposit_status, zoa_deposit_time, zoa_deposit_check, zpy_id, inputtime, category_top, zoa_deposit_check_time', 'numerical', 'integerOnly'=>true),
			array('zoa_deposti', 'numerical'),
			array('zoa_id_card', 'length', 'max'=>20),
			array('zoa_id_pic, zoa_body_pic, zoa_license_pic', 'length', 'max'=>100),
			array('zoa_onreason, zoa_deposit_noreason', 'length', 'max'=>255),
			array('zoa_orderid,zm_name', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zm_id, zoa_type, zca_id, zoa_id_card, zoa_id_pic, zoa_body_pic, zoa_license_pic, zoa_check, zoa_check_time, zoa_accept, zoa_accept_time, zoa_deposit_status, zoa_deposti, zoa_deposit_time, zoa_deposit_check_time, zoa_deposit_check, zoa_deposit_noreason, zpy_id, zoa_orderid, inputtime, category_top', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'category'=>array(self::BELONGS_TO,'Category','zca_id'),
				'member'=>array(self::BELONGS_TO,'Member','zm_id'),
				'payment'=>array(self::BELONGS_TO,'Payment','zpy_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zm_id' => 'Zm',
			'zoa_type' => 'Zoa Type',
			'zca_id' => 'Zca',
			'zoa_id_card' => 'Zoa Id Card',
			'zoa_id_pic' => 'Zoa Id Pic',
			'zoa_body_pic' => 'Zoa Body Pic',
			'zoa_license_pic' => 'Zoa License Pic',
			'zoa_check' => 'Zoa Check',
			'zoa_check_time' => 'Zoa Check Time',
			'zoa_accept' => 'Zoa Accept',
			'zoa_onreason' => 'Zoa Onreason',
			'zoa_accept_time' => 'Zoa Accept Time',
			'zoa_deposit_status' => 'Zoa Deposit Status',
			'zoa_deposti' => 'Zoa Deposti',
			'zoa_deposit_time' => 'Zoa Deposit Time',
			'zoa_deposit_check' => 'Zoa Deposit Check',
			'zoa_deposit_noreason' => 'zoa_deposit_noreason',
			'zoa_deposit_check_time' => 'zoa_deposit_check_time',
			'zpy_id' => 'Zpy',
			'zoa_orderid' => 'Zoa Orderid',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zoa_type',$this->zoa_type);
		$criteria->compare('zoa_id_card',$this->zoa_id_card,true);
		$criteria->compare('zoa_id_pic',$this->zoa_id_pic,true);
		$criteria->compare('zoa_body_pic',$this->zoa_body_pic,true);
		$criteria->compare('zoa_license_pic',$this->zoa_license_pic,true);
		$criteria->compare('zoa_check',$this->zoa_check);
		$criteria->compare('zoa_onreason',$this->zoa_onreason);
		$criteria->compare('zoa_check_time',$this->zoa_check_time);
		$criteria->compare('zoa_accept',$this->zoa_accept);
		$criteria->compare('zoa_accept_time',$this->zoa_accept_time);
		$criteria->compare('zoa_deposit_status',$this->zoa_deposit_status);
		$criteria->compare('zoa_deposti',$this->zoa_deposti);
		$criteria->compare('zoa_deposit_time',$this->zoa_deposit_time);
		$criteria->compare('zoa_deposit_check',$this->zoa_deposit_check);
		$criteria->compare('zoa_deposit_noreason',$this->zoa_deposit_noreason);
		$criteria->compare('zoa_deposit_check_time',$this->zoa_deposit_check_time);
		$criteria->compare('zpy_id',$this->zpy_id);
		$criteria->compare('zoa_orderid',$this->zoa_orderid,true);
		$criteria->compare('inputtime',$this->inputtime);
		
		//根据一级行业分类查找商铺
		if(!empty($this->category_top)){
			$catetop = Category::model()->findByPk($this->category_top);
			$criteria->join = ' LEFT JOIN {{category}} cat ON cat.zca_id=t.zca_id';
			$criteria->addCondition(" (t.zca_id={$this->category_top} OR (t.zca_id IN ({$catetop->zca_arrchildid}))) ");
		}else{
			$criteria->compare('zca_id',$this->zca_id);
		}
		
		//会员名称，用于筛选
		if(!empty($this->zm_name)){
			$criteria->join = ' LEFT JOIN {{member}} m ON m.zm_id=t.zm_id';
			$criteria->addCondition(" m.zm_name like '%".$this->zm_name."%' ");
		}else{
			$criteria->compare('zm_id',$this->zm_id);
		}

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 会员类型
	 */
	public function getZoaType($type=null){
		if($type==null){
			return array(
					'1'=>'个人商家会员',
					'2'=>'企业商家会员',
			);
		}else{
			$level = $this->getZoaType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 保存前的操作
	 */
	public function beforeSave(){
		if($this->isNewRecord){
			$this->zoa_check = 1;//1未审核2审核通过3审核不通过',
			$this->zoa_accept = 1;//1未认可2认可',
			$this->zoa_deposit_status = 1;//保证金缴纳状态:1未缴纳 2缴纳',
			$this->zoa_deposit_check = 1;//保证金审核状态：1未审核2审核通过
			$this->inputtime = time();//申请时间
		}
		return true;
	}
	
	
}